

package ig.formato.dto.basic;

import java.io.Serializable;
import java.lang.reflect.*;

public abstract class DTO
    implements Serializable, Comparable
{

    public DTO()
    {
        orden = Integer.valueOf(0x5f5e0ff);
        id = null;
    }

    public int compareTo(Object o)
    {
        return getOrden().compareTo(((DTO)o).getOrden());
    }

    public Integer getId()
    {
        return id;
    }

    public void setId(Integer id)
    {
        this.id = id;
    }

    public Integer getOrden()
    {
        return orden;
    }

    public void setOrden(Integer orden)
    {
        this.orden = orden;
    }

    public void populate(Object o)
    {
        Object obj[] = new Object[0];
        Method methods[] = o.getClass().getMethods();
        for(int i = 0; i < methods.length; i++)
            try
            {
                String methodName = methods[i].getName();
                if((methodName.startsWith("get") || methodName.startsWith("is")) && Modifier.isPublic(methods[i].getModifiers()) && methods[i].getParameterTypes().length == 0 && !methodName.equals("getClass"))
                {
                    String setMethodName = methodName.replaceFirst(methodName.startsWith("get") ? "get" : "is", "set");
                    getClass().getMethod(setMethodName, new Class[] {
                        methods[i].getReturnType()
                    }).invoke(this, new Object[] {
                        methods[i].invoke(o, obj)
                    });
                }
            }
            catch(IllegalAccessException ilE)
            {
                ilE.printStackTrace();
            }
            catch(InvocationTargetException invE)
            {
                invE.printStackTrace();
            }
            catch(NoSuchMethodException noE) { }

    }

    public abstract String toString();

    private Integer orden;
    private Integer id;
}
